package com.r2coding.server.common.listener;

import com.r2coding.core.utils.IdUtil;
import com.r2coding.server.common.event.log.ErrorLogEvent;
import com.r2coding.server.modules.log.entity.ErrorLog;
import com.r2coding.server.modules.log.service.IErrorLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @author 98k灬
 * @version v1.0.0
 * @Description : 系统错误日志监听器
 * @Create on : 2023/10/18 12:52
 **/
@Component
public class ErrorLogEventListener {
    @Autowired
    private IErrorLogService iErrorLogService;

    @EventListener(ErrorLogEvent.class)
    public void saveErrorLog(ErrorLogEvent event) {
        Long userId = event.getUserId();
        Date currentTime = new Date();
        ErrorLog errorLog = ErrorLog.builder()
                .id(IdUtil.get())
                .logContent(event.getErrorMsg())
                .logStatus(0)
                .createUser(userId)
                .updateUser(userId)
                .createTime(currentTime)
                .updateTime(currentTime)
                .build();
        iErrorLogService.save(errorLog);
    }
}
